package com.md.infrastructure.persistence.repository;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.md.base.BaseRepositoryImpl;
import com.md.domain.model.po.ModulePO;
import com.md.domain.repository.ModuleRepository;
import com.md.infrastructure.persistence.mapper.ModuleMapper;
import org.springframework.stereotype.Repository;

/**
 * @author 刘骄阳
 * 2022-06-12 1:22
 */
@Repository
public class DAOModuleRepository extends BaseRepositoryImpl<ModulePO, ModuleMapper> implements ModuleRepository {

  public DAOModuleRepository(final ModuleMapper mapper) {
    super(mapper);
  }

  @Override
  public ModulePO checkUserModule(final Long moduleId, final String userId) {
    LambdaQueryWrapper<ModulePO> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(ModulePO::getId, moduleId).eq(ModulePO::getUserId, userId);
    return mapper.selectOne(wrapper);
  }

  @Override
  public Boolean checkModuleExists(final String moduleName,
                                   final Long tenantId,
                                   final Long brandId,
                                   final String userId) {
    LambdaQueryWrapper<ModulePO> nameExist = new LambdaQueryWrapper<>();
    nameExist.eq(ModulePO::getModelName, moduleName).eq(ModulePO::getUserId, userId);
    LambdaQueryWrapper<ModulePO> tenantExist = new LambdaQueryWrapper<>();
    tenantExist.eq(ModulePO::getTenantId, tenantId).eq(ModulePO::getBrandId, brandId);
    return mapper.exists(nameExist) || mapper.exists(tenantExist);
  }
}
